package com.bayi.storage.utils;

import java.util.Arrays;

/**
 * @author Administrator on 2023/2/23.
 * <p>
 * 站在顶峰 看世界
 * 落到谷底 思人生
 */
public class ByteUtils {

    /**
     * 字符串转16进制GB18030字符串
     *
     * @param content
     * @return
     */
    public static String alphabeticToHex(String content) {
        String hexStr = "";
        try {
            byte[] arr = content.getBytes("GB18030");
            for (int i = 0; i < arr.length; i++) {
                String str = byteToHex(arr[i]);
                hexStr = hexStr + str;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return hexStr.toUpperCase();
    }

    public static String byteToHex(byte b) {

        String hex = Integer.toHexString(b & 0xFF);
        if (hex.length() < 2) {
            hex = "0" + hex;
        }
        return hex;
    }


    /**
     * 获取16进制字符串
     *
     * @param str
     * @return
     */
    public static String getHexStr(String str) {
        String hex = "0";
        if (!StringUtil.isEmpty(str)) {
            hex = Integer.toHexString(Integer.parseInt(str));
        }
        if (hex.length() == 1) {
            hex = "0" + hex.toUpperCase();
        } else {
            hex = hex.toUpperCase();
        }
        return hex;
    }

    public static String[] get2Hex(String hex) {
        String[] hexSD = new String[1];
        String a = hex.substring(0, 2);
        System.out.println("hex = " + a);
        hexSD[0] = a;
        return hexSD;
    }


    /**
     * hex字符串转byte数组
     *
     * @param inHex 待转换的Hex字符串
     * @return 转换后的byte数组结果
     */
    public static byte[] hexToByteArray(String inHex) {
        int hexlen = inHex.length();
        byte[] result;
        if (hexlen % 2 == 1) {
            //奇数
            hexlen++;
            result = new byte[(hexlen / 2)];
            inHex = "0" + inHex;
        } else {
            //偶数
            result = new byte[(hexlen / 2)];
        }
        int j = 0;
        for (int i = 0; i < hexlen; i += 2) {
            result[j] = hexToByte(inHex.substring(i, i + 2));
            j++;
        }
        return result;
    }

    /**
     * Hex字符串转byte
     *
     * @param inHex 待转换的Hex字符串
     * @return 转换后的byte
     */
    public static byte hexToByte(String inHex) {
        return (byte) Integer.parseInt(inHex, 16);
    }

    /**
     * Hex字符串不满4位补零
     *
     * @param data
     * @return 转换后的byte
     */
    public static String zeroFilling(String data) {
        String content = "";
        if (data.length() == 0) {
            content = "0000";
        } else if (data.length() == 1) {
            content = "000" + data;
        } else if (data.length() == 2) {
            content = "00" + data;
        } else if (data.length() == 3) {
            content = "0" + data;
        } else {
            content = data;
        }
        return content;
    }

    /**
     * Hex字符串不满4位补空格
     *
     * @param data
     * @return 转换后的byte
     */
    public static String spaceFilling(String data) {
        String content = "";
        if (data.length() == 0) {
            content = "2020";
        } else if (data.length() == 1) {
            content = "202020" + data;
        } else if (data.length() == 2) {
            content = "20" + data;
        } else if (data.length() == 3) {
            content = "20" + data;
        } else {
            content = data;
        }
        return content;
    }
//A2A2000200020013000820412042D5D5C6ACD7E9B5B5B0B82CBBE1BCC6B5B5B0B8BBE1BCC6B5B5B0B8A2
//    BBE1BCC6B5B5B0B8
    public static void main(String[] args) {
//        System.out.println("A2A2020201010602A1".length());
        System.out.println(alphabeticToHex("照片组图书,会计图书"));
        System.out.println("D5D5C6ACD7E9B5B5B0B82CBBE1BCC6B5B5B0B8".length());
        System.out.println(Arrays.toString(hexToByteArray("D5D5C6ACD7E9B5B5B0B82CBBE1BCC6B5B5B0B8")));
        System.out.println(hexToByteArray("D5D5C6ACD7E9B5B5B0B82CBBE1BCC6B5B5B0B8").length);
    }
}
